///////////////////////////////////////////////////////////////////////
// File:        TwainScanner.h
// Description: 
// Author:      Philip Kommnick
// Created:     Mon August 31 16:04:48 CEST 2009
//
// This program is open source. For license terms, see the LICENSE file.
//
///////////////////////////////////////////////////////////////////////

#pragma once

class TwainScanner : public Scanner
{
public:
    // TwainPunchImpl
    TwainScanner(Sink &s);
    virtual ~TwainScanner(void);
    virtual bool PreTranslate(MSG &msg);
    virtual void Scan(void);
    virtual bool IsActive(void);
    virtual void Configure(HKEY key);
private:
    bool SelectDriver(TW_IDENTITY &ident);
    void ImageEvent(ULONG eventID);
    void TwainClose(void);
    bool TwainSetCapability(TW_UINT16 cap,TW_UINT16 value,BOOL sign);
    void TwainProcessMessage();
	template<class T> T ConvertFix32(TW_FIX32 &val) {
		return ((val.Whole << 16) | val.Frac) / static_cast<T>(65536);
	}
    TW_UINT16 TwainFunc(TW_IDENTITY *dest, TW_UINT32 DG,TW_UINT16 DAT,TW_UINT16 MSG, TW_MEMREF pData, TW_STATUS *status = NULL);
    TW_IDENTITY imageSrc;
    bool inUse;
    TW_STR32 driverManufacturer;
    TW_STR32 driverProductFamily;
    TW_STR32 driverProductName;
    // twain dll
    static TW_IDENTITY *appIdentity;
    static HMODULE dllHandle;
    static UINT dllRefCnt;
    static DSMENTRYPROC pDSMFunc;
    static BOOL CALLBACK TwainFindWindowHandle(HWND hwnd, LPARAM lParam);
    static std::set<TW_UINT32> openSources;
};

